package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class MemberServiceImpl  implements MemberService {
    @Autowired
    private MemberDao memberDao;
    /**
     * 添加成员
     * @param member
     */
    @Override
    public void add(Member member) {
        //注册用户
        if (member.getPassword()!=null){
        member.setPassword(MD5Utils.md5(member.getPassword()));
        }
      member.setStatus(0);
        memberDao.add(member);

    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
          memberDao.deleteById(id);
    }

    /**
     * 单个查询
     * @param id
     * @return
     */

    @Override
    public Member findById(int id) {
        return memberDao.findById(id);
    }

    /**
     * 修改
     * @param member
     */
    @Override
    public void edit(Member member) {
        memberDao.update(member);
        }


    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    /**
     * 更改状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(int id, int status) {
        Member member = memberDao.findById(id);
        if (member==null){
            throw new RuntimeException("会员数据不存在");
        }
        member.setStatus(status);
     memberDao.update(member);

    }

    @Override
    public Member findTelephone(String phone) {
        return memberDao.findTelephone(phone);
    }


}
